package com.test;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by frinder on 2017/5/17.
 */
public class PatternDemo {

    public static void main(String[] args) {
//        p();
//        findNumberFromString();
//        replaceNumber();
//        split();
//        combine();
//        stringToMap();
        append();
    }


    public static void p() {
        String p = "^\\d+(\\.\\d+)?";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher("1.5");
        System.out.println(matcher.matches());
    }

    public static void findNumberFromString() {
        String p = "(\\D*)(\\d+)(.*)";
        String str = "ab23adfds889afsdkjl3klfd2334kfaslkf";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println(matcher.groupCount());
            for (int i = 0; i <= matcher.groupCount(); i++) {
                System.out.println(matcher.group(i));
            }
        }
    }

    public static void replaceNumber() {
//        String p = "\\d+";
//        String p = "\\d";
        String p = "\\D";
        String str = "ab23adfds889afsdkjl3klfd2334kfaslkf";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        System.out.println(matcher.replaceAll("*"));
    }

    public static void split() {
        String str = "select * from dual where name = ${name} and pwd = ${pwd}";
//        String p = "\\$\\{\\w+}";
        String p = "(\\$\\{\\w+})";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.groupCount());
            for (int i = 0; i <= matcher.groupCount(); i++) {
                System.out.println(matcher.group(i));
            }
        }
        System.out.println(matcher.replaceAll("*"));
    }


    public static void combine() {
        String str = "abc:123;fff:333;ggg:555";
        String p = "(\\w+):(\\d+)";
        Matcher matcher = Pattern.compile(p).matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
            System.out.println(matcher.groupCount());
            for (int i = 0; i <= matcher.groupCount(); i++) {
                System.out.println(matcher.group(i));
            }
        }
    }


    public static void stringToMap() {
        String str = "abc:123;fff:333;ggg:555";
        String p = "(\\w+):(\\d+)";
        Matcher matcher = Pattern.compile(p).matcher(str);
        Map<String, String> map = new HashMap<>();
        while (matcher.find()) {
            int len = matcher.groupCount();
            if (len == 2) {
                map.put(matcher.group(1), matcher.group(2));
            }
        }
        System.out.println(map);
    }


    public static void append() {
        String str = "abc:123;fff:333;ggg:555";
        String p = "(\\w+):(\\d+)";
        Matcher matcher = Pattern.compile(p).matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            int len = matcher.groupCount();
            if (len == 2) {
                matcher.appendReplacement(sb, matcher.group() + ",name:" + matcher.group(1));
            }
        }
        System.out.println(sb);
    }

}
